package ru.evga.inse.server.gwt.user.client.menu.realty;

import java.util.logging.Level;
import java.util.logging.Logger;

import ru.evga.gwt.lib.common.client.view.AbstractEntityListView;
import ru.evga.inse.server.gwt.common.client.domain.dict.CityDto;

import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.text.shared.AbstractRenderer;
import com.google.gwt.text.shared.Renderer;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTMLPanel;


/**
 * The outermost UI of the application.
 */
public class RealtyContentView
    extends AbstractEntityListView<CityDto>
{
	
	Logger logger = Logger.getLogger("RealtyContentView");

    interface Binder extends UiBinder<HTMLPanel, RealtyContentView>
    {
    }

    private static interface GetValue<C>
    {
        C getValue(CityDto city);
    }

    private static final Binder BINDER = GWT.create(Binder.class);

    private static RealtyContentView instance;

    @UiField
    CellTable<CityDto> table;
    @UiField
    Button newButton;

    public static RealtyContentView instance()
    {
        if (instance == null)
        {
            instance = new RealtyContentView();
        }

        return instance;
    }

    public RealtyContentView()
    {
        init(BINDER.createAndBindUi(this), table);
        table.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.DISABLED);

        table.addColumn(new TextColumn<CityDto>()
        {
            public String getValue(CityDto object)
            {
                return String.valueOf(object.getId());
            }
        }, "Id");

        table.addColumn(new TextColumn<CityDto>()
        {
            Renderer<java.lang.String> renderer = new AbstractRenderer<java.lang.String>()
            {
                public String render(java.lang.String obj)
                {
                    return obj == null ? "" : String.valueOf(obj);
                }
            };

            @Override
            public String getValue(CityDto object)
            {
                return renderer.render(object.getName());
            }
        }, "Name");

        // ButtonCell.
        addColumn(new ButtonCell(), "Edit", new GetValue<String>()
        {
            public String getValue(CityDto city)
            {
                return "Edit";
            }
        }, new FieldUpdater<CityDto, String>()
        {
            public void update(int index, CityDto object, String value)
            {
                logger.log(Level.INFO, "this message should get logged");
                getDelegate().onEditClicked(object);
            }
        });
        // ButtonCell.
        addColumn(new ButtonCell(), "Delete", new GetValue<String>()
        {
            public String getValue(CityDto city)
            {
                return "Delete";
            }
        }, new FieldUpdater<CityDto, String>()
        {
            public void update(int index, CityDto object, String value)
            {
                getDelegate().onRemoveClicked(object);
            }
        });

    }


    private <C> void addColumn(Cell<C> cell, String headerText, final GetValue<C> getter, FieldUpdater<CityDto, C> fieldUpdater)
    {
        Column<CityDto, C> column = new Column<CityDto, C>(cell)
        {
            @Override
            public C getValue(CityDto object)
            {
                return getter.getValue(object);
            }
        };
        column.setFieldUpdater(fieldUpdater);

        table.addColumn(column, headerText);
    }

}
